<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT admin FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
?>
<html>
	<head>
		<title>Ages of Strife: Admin user list</title>
		<link type="text/css" rel="stylesheet" href="../global/agesofstrife.css" />
	</head>
	<body>
		<div id="root_container">
<?php
include( "../global/ads.php" );
?>
			<div id="container">
<?php
    include_once( "../global/old_database_connection.php" );
	$query = "SELECT turn, turns_inactive FROM game";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );

    $query = "SELECT name, ip, ban_turn, banned, active_turn, email " .
    		"FROM users " .
    		"JOIN ruler ON ruler.user_id = users.user_id " .
    		"WHERE active_turn > " . ( $row[ 'turn' ] - $row[ 'turns_inactive' ] ) . " ORDER BY ip, active_turn";
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
    $num = mysql_numrows( $result );
    print "<div id=\"main-content\">";
    print( "<a href=\"admin.php\">Back to the admin screen</a><br />\r\n" );
    print( "Current game turn: " . number_format( $row[ 'turn' ] ) . "<br /><br />" );
    if( isset( $_SESSION[ 'ban_ruler_status' ] ) ) {
    	print( $_SESSION[ 'ban_ruler_status' ] . "<br />\r\n" );
    	unset( $_SESSION[ 'ban_ruler_status' ] );
    }    
    print( "\r\n" );
    print "<table><th colspan=\"6\">Ruler list:</th>\r\n";
    print "<tr><td>Name</td><td>IP address</td><td>Email</td><td>Last login</td><td>Ban for ? turns</td><td>Submit ban</td></tr>\r\n";
    $i = 0;
    $even = " id=\"even\"";
    $last_ip = "";
    while ( $i < $num ) {
    	if( $last_ip != mysql_result( $result, $i, "ip" ) ) {
    		if( $even == "" ) {
    			$even = " id=\"even\"";
    		}
    		else {
    			$even = "";
    		}
    		$last_ip = mysql_result( $result, $i, "ip" );
    	}
        print( "<tr" . $even . ">" );
        print( "<td>" . mysql_result( $result, $i, "name" ) . "</td><td>" . mysql_result( $result, $i, "ip" ) . "</td>" .
        		"<td><small>" . mysql_result( $result, $i, "email" ) . "</small></td>" .
        		"<td>" . number_format( mysql_result( $result, $i, "active_turn" ) ) . "</td>" );
        if( mysql_result( $result, $i, "banned" ) == 1 ) {
        	print( "<td>Permanently banned</td>" );
	        print( "<td><form method=\"post\" action=\"ban_ruler.php\">" .
	        		"<input type=\"hidden\" name=\"name\" value=\"" . mysql_result( $result, $i, "name" ) . "\" />" .
	        		"<input type=\"hidden\" name=\"unban\" value=\"unban\" /><input type=\"submit\" value=\"unban\" /></form></td></tr>\r\n" );
        }
        else if( mysql_result( $result, $i, "ban_turn" ) > $row[ 'turn' ] ) {
        	print( "<td>Banned until " . number_format( mysql_result( $result, $i, "ban_turn" ) ) . "</td>" );
	        print( "<td><form method=\"post\" action=\"ban_ruler.php\">" .
	        		"<input type=\"hidden\" name=\"name\" value=\"" . mysql_result( $result, $i, "name" ) . "\" />" .
	        		"<input type=\"hidden\" name=\"unban\" value=\"unban\" /><input type=\"submit\" value=\"unban\" /></form></td></tr>\r\n" );
        }
        else {
	        print( "<td><form method=\"post\" action=\"ban_ruler.php\">" .
	        		"<input type=\"hidden\" name=\"name\" value=\"" . mysql_result( $result, $i, "name" ) . "\" /><input type=\"text\" value=\"48\" size=\"4\" name=\"turns\" />" .
	        		"</td><td><input type=\"submit\" value=\"submit\" /></form></td></tr>\r\n" );
        }
        $i ++;
    }
?>	
					</table>
			    </form>
			</div>
		</div>
	</body>
</html>
	<?php
}
else {
	$_SESSION[ 'admin' ] = 0;
	header( "Location: ../overview/overview.php" );
}
?>